class UserModel {
  /// 用户基本信息
  String? userId;
  String? token;
  String? avatar;
  int? registerDate;
  String? nickname;
  String? mobile;
  String? wxOpenId;

  /// 会员数据
  int? vipEndDate;
  int? score = 0;
  int? totalTime = 0;
  int? temporaryTime = 0;
  String level = "1";

  /// 是否实名
  bool authenticated = false;

  /// 是否付费用户
  bool paid = false;

  UserModel._({this.token});

  factory UserModel.fromToken(String token) {
    return UserModel._(token: token);
  }

  Map<String, dynamic> toJson() {
    return {
      "token": token,
      "userId": userId,
      "avatar": avatar,
      "registerDate": registerDate,
      "nickname": nickname,
      "mobile": mobile,
      "wxOpenId": wxOpenId,
      "vipEndDate": vipEndDate,
      "score": score,
      "totalTime": totalTime,
      "temporaryTime": temporaryTime,
      "level": level,
      "authenticated": authenticated,
      "paid": paid,
    };
  }

  @override
  String toString() {
    return toJson().toString();
  }

  void loadInfoFromLocal(dynamic json) {
    userId = json?["userId"];
    avatar = json?["avatar"];
    registerDate = json?["registerDate"];
    nickname = json?["nickname"];
    mobile = json?["mobile"];
    wxOpenId = json?["wxOpenId"];

    // vipEndDate = json?["vipEndDate"];
    score = json?["score"];
    // totalTime = json?["totalTime"];
    // temporaryTime = json?["temporaryTime"];
    level = json?["level"];

    authenticated = json?["authenticated"] ?? false;
    paid = json?["paid"] ?? false;
  }

  void configInfo(dynamic json) {
    userId = json?['_id'] as String?;
    avatar = json?['avatar_file_url'] as String?;
    registerDate = json?['register_date'] as int?;
    nickname = json?['nickname'] as String?;
    mobile = json?['mobile'] as String?;
    wxOpenId = json?['wx_openid']['app'] as String?;
  }

  /// 配置会员信息
  void configVip(dynamic json) {
    vipEndDate = json?['vip'] as int?;
    score = json?['socre'] as int?;
    totalTime = json?['_t'] as int?;
    temporaryTime = json?['_et'] as int?;
    level = _getLevel(score ?? 0);
  }

  /// 根据经验值计算等级
  String _getLevel(int score) {
    for (int index = 0; index < _levelScores.length; index++) {
      var levelScore = _levelScores[index];
      if (score < levelScore) {
        level = "${index + 1}";
        return level;
      }
    }
    return "max";
  }

  bool dataComplete() {
    return (token?.isNotEmpty ?? false) && (userId?.isNotEmpty ?? false);
  }

  /// 经验等级
  static const _levelScores = <int>[
    // 300以下为1级，依此类推
    // 45级为最高等级，1999999为暂定值，实际经验未设上限
    // 1
    300,
    // 2
    400,
    // 3
    1500,
    // 4
    2400,
    // 5
    3500,
    // 6
    4800,
    // 7
    6300,
    // 8
    8000,
    // 9
    9900,
    // 10
    12000,
    // 11
    14300,
    // 12
    16800,
    // 13
    19500,
    // 14
    22400,
    // 15
    33000,
    // 16
    44200,
    // 17
    56000,
    // 18
    68400,
    // 19
    81400,
    // 20
    95000,
    // 21
    109200,
    // 22
    124000,
    // 23
    139400,
    // 24
    155400,
    // 25
    172000,
    // 26
    189200,
    // 27
    207000,
    // 28
    225400,
    // 29
    244400,
    // 30
    280000,
    // 31
    319200,
    // 32
    362000,
    // 33
    408400,
    // 34
    458400,
    // 35
    512000,
    // 36
    569200,
    // 37
    630000,
    // 38
    694400,
    // 39
    762400,
    // 40
    834000,
    // 41
    909200,
    // 42
    988000,
    // 43
    1070400,
    // 44
    1156400,
    // 45
    1999999
  ];
}
